////
////  MFWSharePlatformPannel.m
////  TravelGuideMdd
////
////  Created by BigPolarBear on 14-5-20.
////  Copyright (c) 2014年 mafengwo.com. All rights reserved.
////
//
//#import "MFWSharePlatformPannel.h"
//
//
//#define BGViewAlpha 0.7
//#define NumberPlatformPerLine   4
//
//// 单个平台入口的总高度
//#define ButtonPlatformHeight    60
//// 单个平台入口的总宽度
//#define ButtonPlatformWidth     40
//
//// 面板水平边距
//#define PannelBorderHorizontal  35
//// 面板顶部垂直边距
//#define PannelBorderVerticalTop 28
//
//// 面板行间距
//#define ButtonPlatformSpace     16
//
//
//// 平台图标高度
//#define ButtonPlatformIconHeight    40
//// 平台图标宽度
//#define ButtonPlatformIconWidth     ButtonPlatformIconHeight
//
//
//typedef void(^MFWSharePlatformPannelCompletion)(MFWShareType type,MFWShareResultState state, NSString* errorMsg);
//
//
//typedef enum
//{
//    PannelContentMediaTypeNews = 0, /**< 新闻 */
//    PannelContentMediaTypeImage = 1, /**< 图片 */
//}
//PannelContentMediaType;
//
//
//@interface MFWSharePlatformPannel()
//
//#pragma mark 内部数据相关
//@property (nonatomic,strong) NSArray* arrayType;
//
//#pragma mark 界面相关
//@property (nonatomic,strong) MFWSharePlatformPannel* strongSelf;
//
//@property (nonatomic,strong) UIWindow* window;
//@property (nonatomic,weak) UIView* bgView;
//@property (nonatomic,weak) UIView* pannel;
//
//
//#pragma mark 用户分享的内容
//@property (nonatomic,copy) MFWSharePlatformPannelCompletion completionBlock;
//
//@property (nonatomic,strong) NSString* text;
//@property (nonatomic,strong) UIImage* image;
//@property (nonatomic,strong) NSString* imageUrl;
//@property (nonatomic,strong) NSString* url;
//@property (nonatomic,strong) NSString* title;
//@property(nonatomic,assign)PannelContentMediaType pannelType;
//
//@end
//
//@implementation MFWSharePlatformPannel
//
//static NSMutableDictionary* dictImage = nil;
//
//
//+(instancetype)pannel
//{
//    return [[MFWSharePlatformPannel alloc] init];
//}
//
//+(void)setType:(MFWShareType)type icon:(UIImage*)icon
//{
//    if (!dictImage) {
//        dictImage = [NSMutableDictionary dictionary];
//    }
//
//    [dictImage setObject:icon forKey:[NSNumber numberWithInteger:type]];
//}
//+(UIImage*)iconOfType:(MFWShareType)type
//{
//    UIImage* icon = dictImage[[NSNumber numberWithInteger:type]];
//    if (!icon) {
//        icon = [[MFWShare sharedInstance] getClientIconWithType:type];
//    }
//    
//    return icon;
//}
//
//-(NSString*)nameOfType:(MFWShareType)type
//{
//    NSString* name = [[MFWShare sharedInstance] getClientNameWithType:type];
//    if ([name isEqualToString:@"微信朋友圈"]) {
//        name = NSLocalizedString(@"朋友圈", nil);
//    }
//    
//    return name;
//}
//
//-(void)shareText:(NSString*)text
//           image:(UIImage*)image
//        imageUrl:(NSString*)imageUrl
//             url:(NSString*)url
//           title:(NSString*)title
//          result:(void(^)(MFWShareType type,MFWShareResultState state, NSString* errorMsg))result
//{
//    self.strongSelf = self;
//    
//    self.text = text;
//    self.image = image;
//    self.imageUrl = imageUrl;
//    self.url = url;
//    self.title = title;
//    
//    self.completionBlock = result;
//    self.pannelType = PannelContentMediaTypeNews;
//
//
//    [self create];
//    
//    [self show];
//}
//
//-(void)shareImageTypeWithText:(NSString*)text
//           image:(UIImage*)image
//        imageUrl:(NSString*)imageUrl
//             url:(NSString*)url
//           title:(NSString*)title
//          result:(void(^)(MFWShareType type,MFWShareResultState state, NSString* errorMsg))result
//{
//    self.strongSelf = self;
//    
//    self.text = text;
//    self.image = image;
//    self.imageUrl = imageUrl;
//    self.url = url;
//    self.title = title;
//    
//    self.completionBlock = result;
//    self.pannelType = PannelContentMediaTypeImage;
//    
//    
//    [self create];
//    
//    [self show];
//}
//
//
//-(void)create
//{
//    // 要分享的平台
//    self.arrayType = [[MFWShare sharedInstance] getConnectedShareTypeArray];
//    
//    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    self.window.hidden = NO;
//    
//    // 背景
////    UIView* bgView = [[UIView alloc] initWithFrame:self.window.bounds];
////    bgView.backgroundColor = [UIColor blackColor];
////    bgView.alpha = BGViewAlpha;
////    self.bgView = bgView;
////    [self.window addSubview:bgView];
//    UIButton* bgButton = [[UIButton alloc] initWithFrame:self.window.bounds];
//    [bgButton addTarget:self action:@selector(buttonCancelClicked:) forControlEvents:UIControlEventTouchUpInside];
//    bgButton.backgroundColor = [UIColor blackColor];
//    bgButton.alpha = BGViewAlpha;
//    self.bgView = bgButton;
//    [self.window addSubview:bgButton];
//    
//    
//    
//    // 面板
//    CGRect framePannel;
//    framePannel.size = [self sizeOfPannel];
//    framePannel.origin = CGPointMake(0, self.window.frame.size.height - framePannel.size.height);
//    
//    UIView* pannel = [[UIView alloc] initWithFrame:framePannel];
//    pannel.backgroundColor = [UIColor whiteColor];
//    self.pannel = pannel;
//    [self.window addSubview:pannel];
//    
//
//
//    // 创建各平台按钮
//    CGFloat buttonBetween = (pannel.frame.size.width - 2 * PannelBorderHorizontal - ButtonPlatformWidth) * 1.0 /(NumberPlatformPerLine-1);
//    [self.arrayType enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//        
//        MFWShareType type = [(NSNumber*)obj integerValue];
//        
//        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.backgroundColor = [UIColor clearColor];
//        button.frame = CGRectMake(0, 0, ButtonPlatformWidth, ButtonPlatformHeight);
//        button.center = CGPointMake(PannelBorderHorizontal+ ButtonPlatformWidth/2 + idx % NumberPlatformPerLine * buttonBetween
//                                    , PannelBorderVerticalTop + ButtonPlatformHeight/2 + (idx/NumberPlatformPerLine)*(ButtonPlatformHeight + ButtonPlatformSpace));
//        button.tag = type;
//        [button addTarget:self action:@selector(buttonPlatformClicked:) forControlEvents:UIControlEventTouchUpInside];
//        [self.pannel addSubview:button];
//
//        
//        UIImageView* iconImgv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ButtonPlatformWidth, ButtonPlatformWidth)];
//        iconImgv.image = [MFWSharePlatformPannel iconOfType:type];
//        [button addSubview:iconImgv];
//        
//        UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0
//                                                                        , iconImgv.frame.size.height + 8
//                                                                        , button.frame.size.width
//                                                                        , button.frame.size.height - (iconImgv.frame.size.height + 8))];
//        titleLabel.font = [UIFont systemFontOfSize:10];
//        titleLabel.text = [self nameOfType:type];
//        titleLabel.textColor = [UIColor grayColor];
//        titleLabel.textAlignment = NSTextAlignmentCenter;
//        [button addSubview:titleLabel];
//        
//        
//        NSLog(@"button:%@",button);
//    }];
//    
//    // 创建顶部分割线
//    UIView* viewSeparator = [[UIView alloc] initWithFrame:CGRectMake(0, 0, pannel.frame.size.width, 2)];
//    viewSeparator.backgroundColor = [UIColor colorWithRed:243/255.0 green:156/255.0 blue:17/255.0 alpha:1];
//    [pannel addSubview:viewSeparator];
//    
////    UIButton* buttonShare = [UIButton buttonWithType:UIButtonTypeCustom];
////    buttonShare.tag = MFWShareTypeSinaWeibo;
////    buttonShare.frame = CGRectMake(buttonCancel.frame.origin.x
////                                   , buttonCancel.frame.origin.y - buttonCancel.frame.size.height
////                                   , buttonCancel.frame.size.width
////                                   , buttonCancel.frame.size.height);
////    [buttonShare setTitle:@"分享到微博" forState:UIControlStateNormal];
////    [buttonShare addTarget:self action:@selector(buttonPlatformClicked:) forControlEvents:UIControlEventTouchUpInside];
////    [pannel addSubview:buttonShare];
//    
//    // 底部按钮区域
//    CGRect frameButtons = CGRectZero;
//    frameButtons.size = [self sizeOfPannelButtons];
//    frameButtons.origin.y = pannel.frame.size.height - frameButtons.size.height;
//    UIView* viewButtons = [[UIView alloc] initWithFrame:frameButtons];
//    [pannel addSubview:viewButtons];
//    
//    // 取消按钮
//    UIButton* buttonCancel = [UIButton buttonWithType:UIButtonTypeCustom];
//    [buttonCancel setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];
//    [buttonCancel setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
//    buttonCancel.titleLabel.font = [UIFont systemFontOfSize:16];
//    buttonCancel.frame = CGRectMake(0, 0, 254, 40);
//    buttonCancel.center = CGPointMake(viewButtons.frame.size.width/2, viewButtons.frame.size.height/2);
//    buttonCancel.layer.cornerRadius = 4;
//    buttonCancel.layer.borderWidth = 1;
//    buttonCancel.layer.borderColor = [[UIColor lightGrayColor] CGColor];
//    [buttonCancel addTarget:self action:@selector(buttonCancelClicked:) forControlEvents:UIControlEventTouchUpInside];
//    [viewButtons addSubview:buttonCancel];
//    
//}
//
//-(void)show
//{
//    self.bgView.alpha = 0;
//    self.pannel.alpha = 0;
//    
//    CGRect frameEnd = self.pannel.frame;
//    CGRect frameBegin = self.pannel.frame;
//    frameBegin.origin.y += frameBegin.size.height;
//    
//    self.pannel.frame = frameBegin;
//    
//    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
//    [UIView animateWithDuration:0.5 animations:^{
//        self.bgView.alpha = BGViewAlpha;
//        self.pannel.alpha = 1;
//        
//        self.pannel.frame = frameEnd;
//    } completion:^(BOOL finished) {
//        [[UIApplication sharedApplication] endIgnoringInteractionEvents];
//    }];
//}
//
//#pragma mark 点击事件
//-(void)buttonCancelClicked:(id)sender
//{
//    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
//
//    [UIView animateWithDuration:0.5 animations:^{
//        // 向下收起
//        CGRect frame = self.pannel.frame;
//        frame.origin.y += frame.size.height;
//        self.pannel.frame = frame;
//        
//        // 淡出
//        self.pannel.alpha = 0;
//        self.bgView.alpha = 0;
//        
//    }completion:^(BOOL finished) {
//        [self exit];
//        if (self.completionBlock) {
//            self.completionBlock(MFWShareTypeAny,MFWShareResultStateCancel,nil);
//        }
//        [[UIApplication sharedApplication] endIgnoringInteractionEvents];
//    }];
//}
//
//-(void)buttonPlatformClicked:(UIButton*)sender
//{
//    MFWShareType type = sender.tag;
//    
//    [UIView animateWithDuration:0.5 animations:^{
//        // 向下收起
//        CGRect frame = self.pannel.frame;
//        frame.origin.y += frame.size.height;
//        self.pannel.frame = frame;
//        
//        // 淡出
//        self.pannel.alpha = 0.2;
//        self.bgView.alpha = 0.2;
//        
//    }completion:^(BOOL finished) {
//        [self exit];
//        
//        
//        switch (self.pannelType) {
//            case PannelContentMediaTypeNews:
//            {
//                // 分享到对应平台
//                [[MFWShare sharedInstance] shareWithType:type
//                                                    text:self.text
//                                                   image:self.image
//                                                imageUrl:self.imageUrl
//                                                     url:self.url
//                                                   title:self.title
//                                                  result:self.completionBlock];
//            
//            }
//                break;
//            case PannelContentMediaTypeImage:
//            {
//                [[MFWShare sharedInstance] shareImageWithType:type
//                                                    text:self.text
//                                                   image:self.image
//                                                imageUrl:self.imageUrl
//                                                     url:self.url
//                                                   title:self.title
//                                                  result:self.completionBlock];
//
//            }
//
//                break;
//                
//            default:{
//            
//            }
//                break;
//        }
//    }];
//}
//
//-(void)exit
//{
//    self.window.hidden = YES;
//    self.window = nil;
//    self.strongSelf = nil;
//}
//
//#pragma mark 界面布局
//-(CGSize)sizeOfPannel
//{
//    
//    NSInteger numberOfLine = ceil(self.arrayType.count * 1.0 / NumberPlatformPerLine);
//    CGFloat heightPlatformButtons = numberOfLine * ButtonPlatformHeight + (numberOfLine - 1) * ButtonPlatformSpace;
//    
//    CGFloat height = PannelBorderVerticalTop+ [self sizeOfPannelButtons].height + heightPlatformButtons;
//    CGFloat width = self.window.frame.size.width;
//    
//    return CGSizeMake(width, height);
//}
//
//-(CGSize)sizeOfPannelButtons
//{
//    return CGSizeMake(self.window.bounds.size.width, 85);
//}
//
//
//@end
